// ------------------------------------------------------------
// @Author: ZhangPeng
// @Date: 2024年12月26日21:55
// ------------------------------------------------------------

using System.Linq;
using UnityEngine;

public class TextureData : UpdatableData
{
    const int textureSize =  512;
    private TextureFormat _textureFormat = TextureFormat.RGB565;
        
    public Layer[] Layers;
        
    public Color[] baseColours;
    [Range(0, 1)]
    public float[] baseStartHeights;
    /// <summary>
    /// 颜色混合参数
    /// </summary>
    [Range(0, 1)]
    public float[] baseBlends;
    
    
    //防止修改着色器时丢失参数
    private float savedMinHeight;
    private float savedMaxHeight;
    
    public void ApplyToMaterial(Material material)
    {
        material.SetInt("layerCount",Layers.Length);
        material.SetColorArray("baseColours",Layers.Select(x=>x.tint).ToArray());
        material.SetFloatArray("baseStartHeights",Layers.Select(x=>x.startHeight).ToArray());
        material.SetFloatArray("baseBlends",Layers.Select(x=>x.blendStrength).ToArray());
        material.SetFloatArray("baseColourStrength",Layers.Select(x=>x.tintStrength).ToArray());
        material.SetFloatArray("baseTextureScale",Layers.Select(x=>x.textureScale).ToArray());
        // Texture2DArray texture2DArray = GenerateTextureArray(Layers.Select(x => x.texture).ToArray());
        // material.SetTexture("baseTextures",texture2DArray);
        // material.SetInt("baseColourCount",baseColours.Length);
        // material.SetColorArray("baseColours",baseColours);
        // material.SetFloatArray("baseStartHeights",baseStartHeights);
        // material.SetFloatArray("baseBlends",baseBlends);
        
        UpdateMeshHeights(material,savedMinHeight,savedMaxHeight);
    }

    /// <summary>
    /// 主线程调用
    /// </summary>
    /// <param name="material"></param>
    /// <param name="minHeight"></param>
    /// <param name="maxHeight"></param>
    public void UpdateMeshHeights(Material material , float minHeight, float maxHeight)
    {
        savedMinHeight = minHeight;
        savedMaxHeight = maxHeight;
        
        material.SetFloat("minHeight",minHeight);
        material.SetFloat("maxHeight",maxHeight);
    }
    
    Texture2DArray GenerateTextureArray(Texture2D[] textures)
    {
        if(textures.Length == 0)
            return null;
        Texture2DArray texture2DArray = new Texture2DArray(textureSize, textureSize, textures.Length, _textureFormat,true);

        for (int i = 0; i < textures.Length; i++)
        {
            texture2DArray.SetPixels(textures[i].GetPixels(),i);
        }
        texture2DArray.Apply();
        return texture2DArray;
    }
    
    
    [System.Serializable]
    public class Layer
    {
        public Texture2D texture;
        public Color tint;
        [Range(0, 1)] 
        public float tintStrength;
        [Range(0, 1)] 
        public float startHeight;
        [Range(0, 1)] 
        public float blendStrength;
        public float textureScale;
    }
    
}